package com.memory.modules.business.service.earn.impl;

import com.memory.common.data.PageSort;
import com.memory.modules.business.domain.earn.CurDayPrice;
import com.memory.modules.business.repository.earn.CurDayPriceRepository;
import com.memory.modules.business.service.earn.CurDayPriceService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @PackageName: com.memory.modules.business.service.earn
 * @ClassName: CurDayPriceService
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/11
 */
@Service
@RequiredArgsConstructor
public class CurDayPriceServiceImpl implements CurDayPriceService {

    private final CurDayPriceRepository curDayPriceRepository;

    @Override
    public CurDayPrice create(CurDayPrice price) {
        return curDayPriceRepository.save(price);
    }

    @Override
    public CurDayPrice modify(Long id, CurDayPrice price) {
        return findById(id).map(e -> {
            e.setId(id);
            curDayPriceRepository.save(price);
            return e;
        }).orElse(null);
    }

    @Override
    public CurDayPrice remove(Long id) {
        return findById(id).map(e -> {
            curDayPriceRepository.delete(e);
            return e;
        }).orElse(null);
    }

    @Override
    public Optional<CurDayPrice> findById(Long id) {
        return curDayPriceRepository.findById(id);
    }

    @Override
    public CurDayPrice findByDate(Date date) {
        return curDayPriceRepository.findByCreateDate(date);
    }

    @Override
    public Page<CurDayPrice> findPage(Example<CurDayPrice> example) {
        final PageRequest pageRequest = PageSort.pageRequest();
        return curDayPriceRepository.findAll(example, pageRequest);
    }

    @Override
    public List<CurDayPrice> findByAccountId(Long id) {
        return curDayPriceRepository.findByAccountId(id);
    }
}
